using System;
using System.Linq;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Widgetsphere.Autos.Websitev2.Objects;
using System.Text;
using Widgetsphere.Autos.API.FacetedNavigation;

namespace Widgetsphere.Autos.Websitev2.UserControls
{
    public partial class ListingCell : System.Web.UI.UserControl
    {
        #region Page Events

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
        }

        #endregion

        #region Methods

        public void Populate(ListingItem item, ListingResults results)
        {
            //var listing = Listing.SelectUsingPK(item.AutoID);
            //var targetUrl = string.Empty;
            //var siteImage = string.Empty;
            //if (listing != null)
            //{
            //targetUrl = "/auto-detail.aspx?id=" + listing.ListingId;
            //targetUrl = listing.Url;
            //if (listing.SiteId != null)
            //{
            //  switch (listing.SiteId.Value)
            //  {
            //    case 1: siteImage = "/images/autotrader.jpg"; break;
            //    case 2: siteImage = "/images/yahoo.png"; break;
            //    case 3: siteImage = "/images/ebay.png"; break;
            //  }
            //}
            //}

            var targetUrl = "/auto-detail.aspx?id=" + item.AutoID;

            var description = item.Description + string.Empty;
            if (description.Length > 150)
                description = description.Substring(0, 150) + "...";

            var modifiedDate = "(Unknown)";
            if (item.PostDate != null)
                modifiedDate = item.PostDate.Value.ToString("MMM dd, yyyy");

            var mileage = "---";
            if (item.Mileage != null)
                mileage = item.Mileage.Value.ToString("###,###,##0");

            var price = "---";
            if (item.Price != null)
                price = item.Price.Value.ToString("$###,###,###,##0");

            var imageURL = "/images/nophoto.jpg";
            var newPic = "/images/stock/" + item.Make.FormatCarImageFile() + "_" + item.Model.FormatCarImageFile() + ".jpg";
            var realFile = Server.MapPath(newPic);
            if (System.IO.File.Exists(realFile))
                imageURL = newPic;

            var sb = new StringBuilder();
            sb.AppendLine("<a href=\"" + targetUrl + "\"><img src=\"" + imageURL + "\" class=\"results-cell-image\" title=\"" + item.Make + " " + item.Model + "\" /></a>");
            sb.AppendLine("<div class=\"results-cell-block\">");
            sb.AppendLine("<div class=\"results-cell-header\">");
            sb.AppendLine("<div class=\"results-header-price\">" + price + "</div>");

            var imgHTML = string.Empty;
            //if (!string.IsNullOrEmpty(siteImage))
            //  imgHTML = "<img href=\"" + siteImage + "\" />";

            sb.AppendLine(imgHTML + "<a href=\"" + targetUrl + "\">" + item.Year + " " + item.Make + " " + item.Model + "</a>");
            sb.AppendLine("</div>");

            if (!string.IsNullOrEmpty(item.State) && !string.IsNullOrEmpty(item.ZIP))
            {
                sb.AppendLine("<div class=\"results-cell-location\">");
                sb.AppendLine(item.State + " " + item.ZIP);
                sb.AppendLine("</div>");
            }

            sb.AppendLine("<table class=\"results-detail\">");
            sb.AppendLine("<tr>");
            sb.AppendLine("<td style=\"width:50%\"><span>Mileage: </span>" + mileage + "</td>");
            sb.AppendLine("<td><span>Make: </span>" + item.Make + "</td>");
            sb.AppendLine("</tr>");
            sb.AppendLine("<tr>");
            sb.AppendLine("<td><span>Model: </span>" + item.Model + "</td>");
            sb.AppendLine("<td><span>Price: </span>" + price + "</td>");
            sb.AppendLine("</tr>");
            sb.AppendLine("<tr>");
            sb.AppendLine("<td><span>Year: </span>" + item.Year + "</td>");
            sb.AppendLine("<td><span>Posted date: </span>" + modifiedDate + "</td>");
            sb.AppendLine("</tr>");
            sb.AppendLine("<tr>");
            sb.AppendLine("<td><span>State: </span>" + item.State + "</td>");
            if (string.IsNullOrEmpty(item.Metro))
                sb.AppendLine("<td></td>");
            else
                sb.AppendLine("<td><span>Metro: </span>" + item.Metro + "</td>");
            sb.AppendLine("</tr>");
            sb.AppendLine("</table>");
            sb.AppendLine("</div>");
            lblHTML.Text = sb.ToString();

        }

        #endregion

    }
}